// pages/gift_detail/gift_detail.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrls: [
      '../image/product_detail/pro1.png',
      '../image/product_detail/pro1.png',
      '../image/product_detail/pro1.png'
    ],
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    R_code: '',
    product_id:''
  },

  // 兑换按钮
  dui: function () {
    var that = this;
    console.log(app.globalData.opid)
    console.log(that.data.detail.goods_id)
    console.log(that.data.detail.exchange_integral)
    wx.showModal({
      title: '提示',
      content: '确定兑换吗？',
      success: function (res) {
        if (res.confirm) {
          console.log('用户点击确定')
          wx.request({
            url: app.globalData.url + 'ziyu.php?Action=duihuanliping',
            data: {
              openid: app.globalData.opid,
              R_productid: that.data.detail.goods_id,
              R_amount: that.data.detail.exchange_integral
            },
            success: function (res) {
              console.log(res)
              if (res.data.success == 0) {
                wx.showToast({
                  title: '账户余额不足',
                  icon: 'none'
                })
              } else {
                that.setData({
                  R_code: res.data.R_code,
                  duihuan_success: true,
                  zhe: true
                })
              }

            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })


  },
  // 我要集赞
  we_jizan:function(e){
    var that = this;
    console.log(e)
    if (!app.globalData.opid == "") {
      wx.showLoading({
        title: '',
      })
      console.log((e.detail.formId == "the formId is a mock one" ? "" : e.detail.formId))
      wx.request({
        url: app.globalData.url + 'collection.php?Action=jizan',
        data: {
          openid: app.globalData.opid,
          R_productid: that.data.product_id,
          formid: (e.detail.formId == "the formId is a mock one"?"":e.detail.formId)
        },
        success:function(res){
          console.log(res)
          wx.hideLoading()
          if(res.data.success == 1){
            wx.showToast({
              title: '参与成功',
              duration: 3000,
              mask: true,
              icon: 'none',
            })
            setTimeout(function(){
              wx.navigateTo({
                url: '../my_jizan_detail/my_jizan_detail?id=' + that.data.product_id + '&d_id=' + res.data.R_id
              })
            },2000)
          
           
            
          }else{
            wx.showToast({
              title: res.data.msg,
              duration: 3000,
              mask: true,
              icon: 'none',
            })
          }
        }
      })
      return false
    }else{
      wx.navigateTo({
        url: '../login/login',
      })
    }
    
  },
  go_jizan_list(){
    wx.navigateTo({
      url: '../my_jizan/my_jizan',
    })
  },
 
  /**
   * 生命周期函数--监听页面加载
   */
  
  onLoad: function (options) {
    var that = this;
    console.log(options)
    // options={
    //   id :商品id
    //   d_id:集赞id
    //   share ：是否分享过来的
    // }
    this.setData({
      product_id:options.id
    })
    wx.request({
      url: app.globalData.url + 'exchange.php?&act=view',
      data: {
        id:options.id
      },
      success: function (res) {
        console.log(res)
        let goods_desc = res.data.goods_desc;
        res.data.goods_desc = goods_desc.replace(/\<img/gi, '<img style="width:100% !important;height:auto!important" ')
        that.setData({
          detail: res.data
        })

      }
    })
   
    
  
    // 图片
    wx.request({
      url: app.globalData.url + 'exchange.php?&act=pictures',
      data: {
        id: options.id
      },
      success: function (res) {
        console.log(res)
        that.setData({
          imgUrls: res.data,
          app_url: app.globalData.img_url
        })

      }
    })
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var that = this;
    return {
      title: '帮我点一下赞哦，就差你了' ,
      path: '/pages/jizan_detail/jizan_detail?id=' + that.data.product_id + "&d_id=" + that.data.d_id + '&share=true'
    }
  }
})